package cate.game.server.cli;

import cate.game.client.msg.MsgOutput;
import cate.game.client.netty.NettyContext;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;

public class NettyContextImpl implements NettyContext {

    private ChannelHandlerContext ctx;

    public NettyContextImpl(ChannelHandlerContext ctx) {
        this.ctx = ctx;
    }

    @Override
    public String cid() {
        return this.ctx.channel().id().asShortText();
    }

    @Override
    public void send(MsgOutput msg) {
        WebSocketFrame frame = MsgEncode.encode(msg);
        this.ctx.write(frame);
    }

    @Override
    public void sendNow(MsgOutput msg) {
        WebSocketFrame frame = MsgEncode.encode(msg);
        this.ctx.writeAndFlush(frame);
    }

    @Override
    public void flush() {
        this.ctx.flush();
    }

    @Override
    public void close(){
        this.ctx.close();
    }
}
